package com.china.produce.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.china.produce.exception.ResultBody;
import com.china.produce.service.impl.OrderMQServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/1/17
 */
@Api(tags = "rabbitmq 生产消息")
@RequestMapping("/produce")
@RestController
public class ProduceController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OrderMQServiceImpl orderMQService;

    @GetMapping("/test")
    public void test()
    {
        orderMQService.sendMsg();
    }

    @ApiOperation("发送消息到topic交换机, 超过15秒死信队列消费")
    @PostMapping("/addMsgToDeadQueue")
    public ResultBody addMsgToDeadQueue()
    {

        HashMap<String, String> smap = new HashMap<>();
        smap.put("msg", "ok");
        smap.put("id", "1000");


        rabbitTemplate.convertAndSend("topic_exchange", "topic.msg", JSON.toJSONString(smap), new CorrelationData(smap.get("id")));

//        rabbitTemplate.convertAndSend("topic_exchange", "topic.ms", "test");
//
//        rabbitTemplate.convertAndSend("deal_topic_exchange", "deal.tx", "tx");

        return ResultBody.success();
    }

    @ApiOperation("发送消息到交换机，消息失败转移到死信队列")
    @PostMapping("/addMsgToAckQueue")
    public ResultBody addMsgToAckQueue()
    {
        rabbitTemplate.convertAndSend("direct_exchange", "sms.ack", "hello");

        return ResultBody.success();
    }
}
